/**
 * 
 */
package com.fpt.edu.vn.h2tv.api.pub;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.dto.Cart;
import com.fpt.edu.vn.h2tv.dto.CartItem;
import com.fpt.edu.vn.h2tv.dto.CartResponse;
import com.fpt.edu.vn.h2tv.service.CartService;
import com.fpt.edu.vn.h2tv.service.UserSessionData;
import com.fpt.edu.vn.h2tv.utils.ApiUtils;

/**
 * @author niteha
 *
 */
@Path("/cart")
public class CartAPI {

    @Path("add")
	@POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_XML)
	public Response add(@FormParam("book_id") int bookId,
			@Context HttpServletRequest httpRequest) {

    	UserSessionData userSessionData = ApiUtils.getUserSessionData(httpRequest.getSession());
    	CartResponse cartResponse = CartService.add(userSessionData, bookId);
    	
		return Response.ok(cartResponse).build();
	}

    @Path("update")
	@POST
    @Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public Response update(Cart cart,
			@Context HttpServletRequest httpRequest) {

    	UserSessionData userSessionData = ApiUtils.getUserSessionData(httpRequest.getSession());
    	CartResponse cartResponse = CartService.update(userSessionData, cart);
    	
		return Response.ok(cartResponse).build();
	}

    @Path("delete")
	@DELETE
    @Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public Response delete(CartItem cartItem,
			@Context HttpServletRequest httpRequest) {

    	UserSessionData userSessionData = ApiUtils.getUserSessionData(httpRequest.getSession());
    	CartResponse cartResponse = CartService.delete(userSessionData, cartItem);
    	
		return Response.ok(cartResponse).build();
	}

    @Path("getCart")
	@GET
	@Produces(MediaType.APPLICATION_XML)
    public Response getCart(@Context HttpServletRequest httpRequest) {

    	UserSessionData userSessionData = ApiUtils.getUserSessionData(httpRequest.getSession());
    	CartResponse cartResponse = CartService.get(userSessionData);
    	
		return Response.ok(cartResponse).build();
    }

    @Path("checkout")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public Response checkout(@Context HttpServletRequest httpRequest, 
    		Cart cart) {

    	UserSessionData userSessionData = ApiUtils.getUserSessionData(httpRequest.getSession());
    	CartResponse cartResponse = CartService.checkout(userSessionData, cart);

    	return Response.ok(cartResponse).build();
    }

    @Path("order")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public Response order(@Context HttpServletRequest httpRequest,
    		Object order) {
    	return null;
    }

    @Path("order/print")
    @GET
    @Produces("application/pdf")
    public Response print(@Context HttpServletRequest httpRequest,
    		long orderId) {
    	return null;
    }

    @Path("payment-method")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response paymentMethod() {

    	return Response.ok(CartService.getPaymentMethodList()).build();
    }

    @Path("shipping-method")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response shippingMethod() {

    	return Response.ok(CartService.getShippingMethodList()).build();
    }

    @Path("order")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getOrder(@Context HttpServletRequest httpRequest,
    		@QueryParam("order-id") String orderId) {

    	return Response.ok(CartService.getOrder(orderId)).build();
    }
}
